import { AssetManager } from "cc";
import { HotUpdate } from "../Asset/hotupdate/Hotupdate";
import UIView from "../Asset/ui/UIView";
import { Macro } from "../Main/Macros";
import c3d from "../tool/cc";
import { Entry } from "./Entry";


/**entry入口代理 */
export class EntryDelegate {
    /**当前有正在加载中的bundle */
    onBundleLoading(versionInfo: HotUpdate.BundleConfig) {
        GM.uiMgr.tip.show(Manager.getLanguage("updating") as string);
    }
    /**发现新版本 */
    onNewVersionFund(versionInfo: HotUpdate.BundleConfig, code: HotUpdate.Code, state: HotUpdate.State) {
        if (versionInfo.isNeedPrompt) {
            let content = Manager.getLanguage(["newVersionForBundle", versionInfo.name]) as string;
            GM.uiMgr.alert.show({
                text: content,
                confirmCb: (isOK) => {
                    let data: HotUpdate.MessageData = {
                        isOk: isOK,
                        state: state,
                        name: versionInfo.name,
                        bundle: versionInfo.bundle,
                    }
                    GM.eventMgr.ins.broadcast(HotUpdate.Event.DOWNLOAD_MESSAGE, data);
                }
            });
        } else {
            GM.assetMgr.hotupdate.hotUpdate();
        }
    }

    /**下载失败 */
    onDownloadFailed(versionInfo: HotUpdate.BundleConfig, code: HotUpdate.Code, state: HotUpdate.State) {
        if (versionInfo.isNeedPrompt) {
            let content = Manager.getLanguage(["newVersionForBundle", versionInfo.name]) as string;
            GM.uiMgr.alert.show({
                text: content,
                confirmCb: (isOK) => {
                    let data: HotUpdate.MessageData = {
                        isOk: isOK,
                        state: state,
                        name: versionInfo.name,
                        bundle: versionInfo.bundle,
                    }
                    GM.eventMgr.ins.broadcast(HotUpdate.Event.DOWNLOAD_MESSAGE, data);
                }
            });
        } else {
            GM.assetMgr.hotupdate.downloadFailedAssets();
        }
    }

    /**当前已经是新包，无需更新 */
    onAreadyUpToData(versionInfo: HotUpdate.BundleConfig, code: HotUpdate.Code, state: HotUpdate.State) {
        GM.uiMgr.loading.show(0.1,Manager.getLanguage("loading_game_resources"))
        GM.assetMgr.bundl.loadBundle(this);
    }

    /**下载版本文件失败 */
    onDownloadManifestFailed(versionInfo: HotUpdate.BundleConfig, code: HotUpdate.Code, state: HotUpdate.State) {
        let content = Manager.getLanguage("downloadFailManifest") as string;
        if (code == HotUpdate.Code.ERROR_NO_LOCAL_MANIFEST) {
            content = Manager.getLanguage("noFindManifest") as string;
        } else if (code == HotUpdate.Code.ERROR_PARSE_MANIFEST) {
            content = Manager.getLanguage("manifestError") as string;
        }
        GM.uiMgr.tip.show(content);
    }

    /**正在检测更新 */
    onCheckingVersion(versionInfo: HotUpdate.BundleConfig, code: HotUpdate.Code, state: HotUpdate.State) {
        //do nothing
    }

    /**其它错误 */
    onOtherReason(versionInfo: HotUpdate.BundleConfig, code: HotUpdate.Code, state: HotUpdate.State) {
        //do nothing
    }

    /**资源下载中 */
    onDownloading(versionInfo: HotUpdate.BundleConfig, info: HotUpdate.DownLoadInfo) {
        let newPercent = 0;
        let config = GM.assetMgr.hotupdate.getBundleName(versionInfo.bundle);
        if (info.code == HotUpdate.Code.UPDATE_PROGRESSION) {
            newPercent = info.percent == Number.NaN ? 0 : info.percent;
        } else if (info.code == HotUpdate.Code.ALREADY_UP_TO_DATE) {
            newPercent = 1;
        } else if (info.code == HotUpdate.Code.UPDATE_FINISHED) {
            newPercent = 1.1;
            c3d.log(`正在加载${config.name}`);
            GM.assetMgr.bundl.loadBundle(this);
        } else if (info.code == HotUpdate.Code.UPDATE_FAILED || info.code == HotUpdate.Code.ERROR_NO_LOCAL_MANIFEST ||
            info.code == HotUpdate.Code.ERROR_DOWNLOAD_MANIFEST || info.code == HotUpdate.Code.ERROR_PARSE_MANIFEST ||
            info.code == HotUpdate.Code.ERROR_DECOMPRESS) {
            newPercent = -1;
        } else return;
        GM.eventMgr.ins.broadcast(HotUpdate.Event.DOWNLOAD_PROGRESS, { progress: newPercent, config: config })
    }

    /**加载Bundle错误 */
    onLoadBundleError(versionInfo: HotUpdate.BundleConfig, err: Error) {
        GM.uiMgr.loading.hide();
        let content = Manager.getLanguage(["updateFaild", versionInfo.name]) as string;
        GM.uiMgr.tip.show(content);
    }

    /**加载bundle完成 */
    onLoadBundleComplete(versionInfo: HotUpdate.BundleConfig, bundle: AssetManager.Bundle) {
        //通知入口管理进入bundle
        GM.uiMgr.loading.hide();
        GM.entryMgr.onLoadBundleComplete(versionInfo, bundle);
    }


    /**进入bundle完成 */
    onEnterGameView(entry: Entry | null, gameView: UIView) {
        //删除除自己之外的其它bundle
        let excludeBundles = this.getPersistBundle();
        if (entry) {
            excludeBundles.push(entry.bundle);
        }

        //进入下一场景，关闭掉当前的场景
        if (GM.uiMgr.curUIView) {
            GM.uiMgr.curUIView.close();
        }
        GM.uiMgr.curUIView = gameView;

        GM.assetMgr.bundl.removeLoadedBundle(this, excludeBundles);
    }

    onShowGameView(entry: Entry | null, gameView: UIView) {

    }

    /**主包检测更新 */
    onCheckUpdate() {
        let config = new HotUpdate.BundleConfig(
            Manager.getLanguage("mainPack"),
            Macro.BUNDLE_RESOURCES,
            true
        );
        GM.assetMgr.bundl.enterBundle(config, this);
    }

    /**获取常驻于内存不释放的bundle */
    getPersistBundle() {
        return [Macro.BUNDLE_RESOURCES];
    }

    onQuitGame(mainEntry: Entry | null) {
        if (mainEntry) {
            if (GM.uiMgr.curUIView) {
                GM.uiMgr.curUIView.close();
            }
            mainEntry.onEnter(true);
        }
    }

    getEntryConfig(bundle: BUNDLE_TYPE): HotUpdate.BundleConfig | null {
        return null;
    }
}